<?php
/**
 * 任务小应用
 * (c)2010-2099
 */

!defined('Q_PATH') && exit;

class UserModel extends Model{

	static public function init__(){

		return array(
			'table_name'=>'user',
			'check'=>array(
				'user_name'=>array(
					array('require','用户名不能为空'),
					array('min_length',5,'用户名不能少于 5 个字符'),
					array('max_length',15,'用户名不能超过 15 个字符'),
				),
				'user_password'=>array(
					array('require','密码不能为空'),
					array('min_length',6,'密码不能少于 6 个字符'),
					array('max_length',50,'密码不能超过 50 个字符'),
				),
			),
		);
	}

	static function F(){
		$arrArgs=func_get_args();
		return ModelMeta::instance(__CLASS__)->findByArgs($arrArgs);
	}

	static function M(){
		return ModelMeta::instance(__CLASS__);
	}

	protected function beforeSave_(){
		$this->user_name=C::text($this->user_name);
	}

	protected function beforeCreate_(){
		// 验证用户是否唯一
		if(self::$_arrMeta[$this->_sClassName]->find(array('user_name'=>$this->user_name))->getCounts()>=1){
			Q::E(Q::L('用户名%s只能够唯一','__QEEPHP__@Q',null,$this->user_name));
		}

		// 保存数据
		$this->changeProp('user_password',$this->encodePassword_($this->user_password));
		$this->changeProp('user_status',1);
	}

	private function encodePassword_($sCleartext){
		return md5($sCleartext);
	}

	public function createTask($sSubject,$sDescription=null){   
		$oTask=new TaskModel();
		$oTask->user_id=$GLOBALS['___login___']['user_id'];
		$oTask->task_subject=$sSubject;
		$oTask->task_description=$sDescription;
		return $oTask;
	}

}
